var gulp = require('gulp');
var browserSync = require('browser-sync');
var selenium = require('selenium-standalone');
var webdriver = require('gulp-webdriver');
// var mocha = require('gulp-mocha');

gulp.task('serve:test', function(done) {
    browserSync({
        logLevel: 'silent',
        notify: false,
        open: false,
        port: 9000,
        server: {
            baseDir: ['tests']
        },
        ui: false
    }, done);
});

gulp.task('selenium', function(done) {
    selenium.install({
        version: '2.46.0',
        baseURL: 'http://npm.taobao.org/mirrors/selenium',
        drivers: {
            chrome: {
                // check for more recent versions of chrome driver here:
                // http://chromedriver.storage.googleapis.com/index.html
                version: '2.16',
                arch: process.arch,
                baseURL: 'http://npm.taobao.org/mirrors/chromedriver'
            },
            ie: {
                // check for more recent versions of internet explorer driver here:
                // http://selenium-release.storage.googleapis.com/index.html
                version: '2.46.0',
                arch: process.arch,
                baseURL: 'http://npm.taobao.org/mirrors/selenium'
            }
        },
        logger: function(message) {
            console.log(message)
        }
    }, function(err) {
        if (err) return done(err);
        selenium.start(function(err, child) {
            if (err) return done(err);
            selenium.child = child;
            done();
        });
    });
});

gulp.task('test', ['serve:test','selenium'], function() {
    return gulp.src('tests/specs/**/*.js', {
        read: false
    }).pipe(webdriver({
        desiredCapabilities: {
            browserName: 'chrome'
        }
    })).once('end', function() {
        browserSync.exit();
        // selenium.child.kill();
    });
});